package com.example.demo.zaqizabatest;

import com.example.demo.user.entity.User;

import java.io.*;
import java.net.*;

/**
 * @ClassName ServerSocketTest
 * @Author l42142
 * @Date 2021/3/5 10:30
 * @Description TODO
 * @Version 1.0
 **/
@SuppressWarnings("AlibabaAvoidManuallyCreateThread")
public class ServerSocketTest {
    public static void main(String[] args) {

        // 一、TCP 协议

        // 测试 1

        /*ServerSocket serverSocket = null;
        Socket socket = null;
        InputStream in = null;
        OutputStream out = null;
        int port = 5000;
        try {
            serverSocket = new ServerSocket(port);    //创建服务器套接字
            System.out.println("服务器开启，等待连接。。。");
            socket = serverSocket.accept();    //获得连接
            //接收客户端发送的内容
            in = socket.getInputStream();
            byte[] b = new byte[1024];
            int len = in.read(b);
            System.out.println("客户端发送的内容为：" + new String(b, 0, len));

            out = socket.getOutputStream();
            out.write("我是服务器端".getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
                if (out != null) {
                    out.close();
                }
                if (serverSocket != null) {
                    serverSocket.close();
                }
                if (socket != null) {
                    socket.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }*/

        // 测试2

        /*ServerSocket serverSocket = null;
        int port = 5000;
        Socket clientSocket = null;
        BufferedReader in = null;
        String str = null;

        try {
            serverSocket = new ServerSocket(port);    //创建服务器套接字
            System.out.println("服务器开启，等待连接。。。");
            clientSocket = serverSocket.accept();// 获得链接
            //接收客户端发送的内容
            in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
            while (true) {
                str = in.readLine();
                System.out.println("客户端发送的内容为：" + str);
                Thread.sleep(2000);
            }
        } catch (IOException | InterruptedException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }*/


        // 测试3

        // 监听10000端口
        /*ServerSocket server = null;
        try {
            server = new ServerSocket(10000);
            while (true) {
                //接收客户端的连接
                Socket socket = server.accept();
                //调用客户端的数据处理方法
                invoke(socket);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }*/


        // 二、UDP协议

        // 测试1

        DatagramSocket ds = null;
        DatagramPacket dpReceive = null;
        int port = 3021;
        try {
            ds = new DatagramSocket(port);
            System.out.println("UDP服务器已启动。。。");
            byte[] b = new byte[1024];
            while (ds.isClosed() == false) {
                dpReceive = new DatagramPacket(b, b.length);
                try {
                    ds.receive(dpReceive);
                    byte[] data = dpReceive.getData();
                    int len = data.length;
                    System.out.println("UDP客户端发送的内容是：" + new String(data, 0, len).trim());
                    System.out.println("UDP客户端IP：" + dpReceive.getAddress());
                    System.out.println("UDP客户端端口：" + dpReceive.getPort());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        } catch (SocketException e1) {
            // TODO 自动生成的 catch 块
            e1.printStackTrace();
        }
    }

    private static void invoke(final Socket socket) throws IOException {
        //开启一个新线程
        new Thread(new Runnable() {
            @Override
            public void run() {
                //创建输入流对象
                ObjectInputStream is = null;
                //创建输出流对象
                ObjectOutputStream os = null;
                try {
                    is = new ObjectInputStream(socket.getInputStream());
                    os = new ObjectOutputStream(socket.getOutputStream());
                    //读取一个对象
                    Object obj = is.readObject();
                    //将对象转换为User类型
                    User user = (User) obj;
                    //在服务器端输出name成员和password成员信息
                    System.out.println("user: " + user.getName() + "/" + user.getAge());
                    //修改当前对象的name成员数据
                    user.setName(user.getName() + "_new");
                    //修改当前对象的password对象数据
                    user.setAge(user.getAge() + "_new");
                    //将修改后的对象输出给客户端
                    os.writeObject(user);
                    os.flush();
                } catch (IOException | ClassNotFoundException ex) {
                    ex.printStackTrace();
                } finally {
                    try {
                        //关闭输入流
                        is.close();
                        //关闭输出流
                        os.close();
                        //关闭客户端
                        socket.close();
                    } catch (Exception ex) {
                    }
                }
            }
        }).start();
    }

}
